﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HYSpace.SpaceManager.Manager
{
	internal class ZoneManager
	{
		public ZoneManager(ExternalCommandData commandData, Dictionary<int, List<Zone>> zoneData)
		{
			this.m_commandData = commandData;
			this.m_zoneDictionary = zoneData;
		}

		public void CreateZone(Level level, Phase phase)
		{
			Zone zone = this.m_commandData.Application.ActiveUIDocument.Document.Create.NewZone(level, phase);
			if (zone != null)
			{
				this.m_zoneDictionary[level.Id.IntegerValue].Add(zone);
			}
		}

		public Zone CreateZoneEx(Level level, Phase phase)
		{
			Zone zone = this.m_commandData.Application.ActiveUIDocument.Document.Create.NewZone(level, phase);
			if (zone != null)
			{
				this.m_zoneDictionary[level.Id.IntegerValue].Add(zone);
				zone.CoolingSetPoint = UnitConvert.CovertToAPI(25.0, (Autodesk.Revit.DB.DisplayUnitType)57);
				zone.DehumidificationSetPoint = UnitConvert.CovertToAPI(60.0, (Autodesk.Revit.DB.DisplayUnitType)19);
				zone.HeatingSetPoint = UnitConvert.CovertToAPI(20.0, (Autodesk.Revit.DB.DisplayUnitType)57);
				zone.HumidificationSetPoint = UnitConvert.CovertToAPI(60.0, (Autodesk.Revit.DB.DisplayUnitType)19);
				return zone;
			}
			return null;
		}

		public void AddSpaces(SpaceSet spaces)
		{
			this.m_currentZone.AddSpaces(spaces);
		}

		public void RemoveSpaces(SpaceSet spaces)
		{
			this.m_currentZone.RemoveSpaces(spaces);
		}

		public List<Zone> GetZones(Level level)
		{
			this.m_currentLevel = level;
			return this.m_zoneDictionary[level.Id.IntegerValue];
		}

		public bool DeleteZone(Zone zone)
		{
			foreach (KeyValuePair<int, List<Zone>> keyValuePair in this.m_zoneDictionary)
			{
				if (keyValuePair.Value.Contains(zone))
				{
					return this.m_zoneDictionary[keyValuePair.Key].Remove(zone);
				}
			}
			return false;
		}

		public Zone CurrentZone
		{
			get
			{
				return this.m_currentZone;
			}
			set
			{
				this.m_currentZone = value;
			}
		}

		private ExternalCommandData m_commandData;

		private Dictionary<int, List<Zone>> m_zoneDictionary;

		private Level m_currentLevel;

		private Zone m_currentZone;
	}
}
